package tech.aistar.prj.service.impl;

import org.apache.ibatis.session.SqlSession;
import tech.aistar.prj.mapper.VideoMapper;
import tech.aistar.prj.model.dto.VideoDTO;
import tech.aistar.prj.model.dto.VideoQueryDTO;
import tech.aistar.prj.model.pojo.User;
import tech.aistar.prj.model.pojo.Video;
import tech.aistar.prj.service.IUserService;
import tech.aistar.prj.service.IVideoService;
import tech.aistar.prj.util.MybatisUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class VideoServiceImpl implements IVideoService {
    private IUserService userService = new UserServiceImpl();
    //第二种方案 - userMapper √
    @Override
    public List<VideoDTO> getAll() {
        SqlSession sqlSession = null;
        List<VideoDTO> videoList = new ArrayList<>();

        try {
            sqlSession = MybatisUtil.getSession();
            VideoMapper videoMapper = sqlSession.getMapper(VideoMapper.class);

            //推荐直接在此处使用其他的mapper -
            //当需要进行事务控制的事务
            //UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<Video> videos = videoMapper.findAll();

            //对该集合点赞量进行升序
            videos.sort(Comparator.comparing(Video::getLikes));

            videos.forEach(v->{
                //组
                VideoDTO dto = new VideoDTO();
                dto.setVideo(v);//苏新华
                //绑定这个视频v对应的用户信息
                User u = userService.getById(v.getUserId());//华静

                dto.setUsername(u.getUsername());
                //班级
                videoList.add(dto);
            });

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return videoList;
    }

    @Override
    public List<VideoDTO> findMoreConditional(VideoQueryDTO videoDTO) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        List<VideoDTO> list = null;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            list = videoMapper.findMoreConditional(videoDTO);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public List<Video> searchByUser(String username) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        List<Video> list = null;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            list = videoMapper.findByUser(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public int release(Video video) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.save(video);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

    @Override
    public int changeStatus(Integer id, Integer userId,Integer status) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.updateStatus(id,userId,status);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

    @Override
    public int updateVideoWithUser(Video v) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.updateVideoWithUser(v);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

    @Override
    public Video getById(Integer id) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        Video v = null;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            v= videoMapper.getById(id);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return v;
    }


}
